/*******************************************************************************
 * Package: com.hngtrust.iir.report.service.impl
 * Type:    ReportCollectionServiceImpl
 * Date:    2021/9/13 16:11
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.material.service.impl;

import com.hngtrust.framework.id.service.MyIdGenerator;
import com.hngtrust.framework.page.PageResult;
import com.hngtrust.framework.page.service.PageService;
import com.hngtrust.framework.security.util.SecurityUtils;
import com.hngtrust.iir.dto.ReqDTO;
import com.hngtrust.iir.material.controller.dto.MaterialCollectDeleteVO;
import com.hngtrust.iir.material.controller.dto.MaterialCollectInsertVO;
import com.hngtrust.iir.material.controller.dto.MaterialCollectQueryVO;
import com.hngtrust.iir.material.dto.MaterialCollectAndPathDTO;
import com.hngtrust.iir.material.entity.MaterialCollect;
import com.hngtrust.iir.material.service.MaterialCollectService;
import com.hngtrust.iir.material.service.MaterialCollectionService;
import com.hngtrust.iir.report.service.ReportCollectService;
import com.hngtrust.iir.user.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 内部资料（我的收藏）
 *
 * @author Chaidd
 * @date 2021/11/10 16:11
 */
@Service
@Slf4j
public class MaterialCollectionServiceImpl implements MaterialCollectionService {

    @Autowired
    private PageService pageService;
    @Autowired
    private MyIdGenerator myIdGenerator;
    @Value("${tiger.minio.out.address}")
    private String url;
    @Autowired
    private ReportCollectService collectService;

    @Autowired
    private MaterialCollectService materialCollectService;



    /**
     * 内部研报-我的收藏列表
     * @param reqDTO
     * @return
     */
    @Override
    public PageResult<MaterialCollectAndPathDTO> listCollect(ReqDTO<MaterialCollectQueryVO> reqDTO){
        Date createTime = reqDTO.getContent().getCreateTime();
        MaterialCollect materialCollect = new MaterialCollect();
        if(createTime != null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
            String format1 = format.format(createTime);
            materialCollect.setCreateTimeDt(format1);
        }
        //当前登录人的收藏
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        materialCollect.setCreateUserId(loginUser.getId());
        
        PageResult<MaterialCollectAndPathDTO> pageResult = pageService.page(reqDTO.getPageParam(),()-> materialCollectService.queryListAndPath(materialCollect));
        if(pageResult.getRows().size() > 0){
            for(MaterialCollectAndPathDTO p : pageResult.getRows()){
                p.setFilePath(url+p.getFilePath());
                List<MaterialCollect> materialCollects = materialCollectService.queryByReportId(p.getReportId(),materialCollect.getCreateUserId());
                if (materialCollects.isEmpty()) {
                    p.setCollection(2);
                } else {
                    p.setCollection(1);
                }
            }
        }



        return pageResult;
    }
    
    /**
     * 内部研报-删除收藏列表
     * @param reqDT
     * @return
     */
    @Override
    public void deleteCollect(MaterialCollectDeleteVO reqDT) {
        materialCollectService.deleteById(reqDT.getId());
    }
    
    /**
     * 内部研报-新增收藏列表
     * @param reqDT
     */
    @Override
    public void addCollect(MaterialCollectInsertVO reqDT) {
        MaterialCollect materialCollect = new MaterialCollect();
        materialCollect.setId(myIdGenerator.nextId());
        materialCollect.setTitle(reqDT.getTitle());
        materialCollect.setReportId(reqDT.getReportId());
        materialCollect.setCreateTime(new Date());
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        materialCollect.setCreateUserId(loginUser.getId());
        materialCollectService.add(materialCollect);
    }
}
